const zmfDbClient = uniCloud.importObject("zmf-db-client");

const types = {
	// 设置内容
	setInput: "setInput",
	// 清空内容
	clear: "clear",
	// 初始化配音员信息
	initVoice: "initVoice",
	// 选择配音员
	changeVoice: "changeVoice",
	// 合成语音
	textToSpeech: "textToSpeech",
	// 新增试听记录
	addAuditionRecord: "addAuditionRecord",
	// 长文本合成
	longTextToSpeech:"longTextToSpeech"
}
export default {
	namespaced: true,
	state: {
		delta: undefined,
		html: undefined,
		text: undefined,
		// 情感程度
		emotion: 100,
		// 音速
		sonicSpeed: 1,
		// 音量
		volume: 5,
		// 音量
		fileType: "mp3",
		// 配音员
		voice: {
			type: [1],
			source: 1,
			imageType: 1,
			name: "智逍遥-专业说书人",
			id: 10510000,
			label: [{
					text: "通用",
					backgroundColor: "#CCC"
				},
				{
					text: "专业旁白",
					backgroundColor: "#aaaa00"
				}
			],
			isEmotional: false,
			create_date: 1684808636000,
			url: []
		},
		selectIndex: 1,
		voiceList: [],
		// 多音字
		identifyData: undefined,
		// 最大合成长度
		maxSynthesisLength: 3001,
		// 语音播放对象
		audioPlayObjId:null,
		
	},
	getters: {

	},
	mutations: {
		[types.setInput](state, {
			delta,
			html,
			text
		}) {
			state.delta = delta
			state.html = html
			state.text = text
		},
		[types.clear](state) {
			state.delta = undefined
			state.html = undefined
			state.text = undefined
		},
		async [types.initVoice](state) {
			if (state.voiceList && state.voiceList.length === 0) {
				const resp = await zmfDbClient.getVoiceInfo();
				const {
					errCode,
					userRecord
				} = resp
				if (userRecord !== undefined && userRecord.length > 0) {
					state.voiceList = [{
							name: '收藏',
							voices: [],
						},
						{
							name: '通用',
							voices: [],
						},
						{
							name: '多情感',
							voices: [],
						},
						{
							name: '客服',
							voices: [],
						},
						{
							name: '阅读',
							voices: [],
						},
						{
							name: '咨询',
							voices: [],
						},
						{
							name: '对话',
							voices: [],
						},
						{
							name: '外语',
							voices: [],
						},
						{
							name: '方言',
							voices: [],
						},
						{
							name: '特色',
							voices: [],
						}
					]

					for (let item of userRecord) {
						if (item && item.type) {
							for (let itemType of item.type) {
								state.voiceList[itemType].voices.push(item)
							}
						}
					}
				}
			}
		},
		[types.changeVoice](state, {
			data
		}) {
			state.voice = data
		},
		async [types.textToSpeech](state, {
			uid,
			that,
			text,
			fileID,
			SessionId,
			Subtitles,
			recordstate
		}) {
			// 创建使用记录
			const date = new Date().getTime();
			const recordParam = {
				uid: uid,
				type: 1,
				state: recordstate,
				text: state.text,
				html: state.html,
				url: fileID,
				use_words: text.length,
				create_date: date,
				expire_date: date + 1000 * 60 * 60 * 24 * 7,
				sessionId: SessionId,
				subtitles: Subtitles
			}
			await zmfDbClient.addRecordInfo(recordParam)

			// 修改使用字符数
			await that.$store.dispatch('loginStore/useWords', {
				uid,
				useWords: text.length
			});

			state.delta = undefined
			state.html = undefined
			state.text = undefined
			state.identifyData = undefined
		},
		async [types.addAuditionRecord](state, {
			uid,
			that,
			text,
			SessionId,
			Subtitles
		}) {
			// 创建使用记录
			const date = new Date().getTime();
			const recordParam = {
				uid: uid,
				type: 2,
				state: 3,
				text: text,
				html: "",
				use_words: text.length,
				create_date: date,
				expire_date: date + 1000 * 60 * 60 * 24 * 7,
				sessionId: SessionId,
				subtitles: Subtitles
			}
			await zmfDbClient.addRecordInfo(recordParam)
		
			// 修改使用字符数
			await that.$store.dispatch('loginStore/useWords', {
				uid,
				useWords: text.length - 1
			});
		
		},
	
	},
	actions: {
		setInput({
			commit
		}, {
			delta,
			html,
			text
		}) {
			commit(types.setInput, {
				delta,
				html,
				text
			})
		},
		clear({
			commit
		}) {
			commit(types.clear)
		},
		initVoice({
			commit
		}) {
			commit(types.initVoice)
		},
		changeVoice({
			commit
		}, {
			data
		}) {
			commit(types.changeVoice, {
				data
			})
		},
		async textToSpeech({
			commit
		}, {
			uid,
			that,
			text,
			fileID,
			SessionId,
			Subtitles,
			recordstate
		}) {
			
			if (recordstate == null || recordstate === undefined) {
				recordstate = 3
			}
			else {
				recordstate = recordstate
			}
			await commit(types.textToSpeech, {
				uid,
				that,
				text,
				fileID,
				SessionId,
				Subtitles,
				recordstate
			})
		},
		async addAuditionRecord({
			commit
		}, {
			uid,
			that,
			text,
			SessionId,
			Subtitles
		}) {
			await commit(types.addAuditionRecord, {
				uid,
				that,
				text,
				SessionId,
				Subtitles
			})
		}
	}
}